﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using MyWeb.ServiceBus.Common;
using MyWeb.ServiceBus.Model.Dto;
using MyWeb.ServiceBus.Model.Models;
using MyWeb.ServiceBus.Services.Db;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace MyWeb.ServiceBus.Services.Impl
{
    public class InterfaceService : BaseEFService<Interface>, IInterfaceService
    {
        Context db;
        public InterfaceService(Context context) : base(context)
        {
            db = context;
        }

        public async Task<List<InterfaceDto>> QueryPageAsync(InterfaceDto dto)
        {
            var where = ExtLinq.True<InterfaceDto>();
            if (!string.IsNullOrEmpty(dto.Key))
            {
                where = where.And(o => o.Name.Contains(dto.Key));
            }
            var linq = db.Interface
                .Select(a => new InterfaceDto
                {
                    Id = a.Id,
                    ApplicationId = a.ApplicationId,
                    Name = a.Name,
                    Status = a.Status,
                    Method = a.Method,
                    Path = a.Path,
                    Classification = a.Classification,
                    Remark = a.Remark,
                    CreateDate = a.CreateDate,
                    CreateUserId = a.CreateUserId,
                    UpdateDate = a.UpdateDate,
                    UpdateUserId = a.UpdateUserId,
                })
                .Where(where);
            var result = new List<InterfaceDto>();
            if(dto.Pagination != null)
            {
                dto.Pagination.DataCount = await linq.CountAsync();
                result = await linq
                    .Skip((dto.Pagination.Page - 1) * dto.Pagination.PageSize)
                    .Take(dto.Pagination.PageSize)
                    .ToListAsync();
            }
            else
            {
                result = await linq.ToListAsync();
            }
            return result;
        }
    }
}
